function observe(obj) {
  for (const key in obj) {
    let internalValue = obj[key];
    const funcs = [];
    Object.defineProperty(obj, key, {
      get() {
        // 依赖收集 记录：是那个函数在用我
        if (window.__func && !funcs.includes(window.__func)) {
          funcs.push(window.__func);
        }
        return internalValue;
      },
      set(value) {
        internalValue = value;
        // 派发更新，运行：自动调用依赖该属性的函数
        funcs.forEach((f) => typeof f === "function" && f());
      },
    });
  }
}

function autorun(fn) {
  window.__func = fn;
  fn();
  window.__func = null;
}
